package Integral

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/integral"
	IntegralReq "github.com/flipped-aurora/gin-vue-admin/server/model/integral/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type IntegralsApi struct{}

// CreateIntegrals 创建积分表
// @Tags Integrals
// @Summary 创建积分表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Integral.Integrals true "创建积分表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /integrals/createIntegrals [post]
func (integralsApi *IntegralsApi) CreateIntegrals(c *gin.Context) {
	var integrals Integral.Integrals
	err := c.ShouldBindJSON(&integrals)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = integralsService.CreateIntegrals(&integrals)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteIntegrals 删除积分表
// @Tags Integrals
// @Summary 删除积分表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Integral.Integrals true "删除积分表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /integrals/deleteIntegrals [delete]
func (integralsApi *IntegralsApi) DeleteIntegrals(c *gin.Context) {
	id := c.Query("id")
	err := integralsService.DeleteIntegrals(id)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteIntegralsByIds 批量删除积分表
// @Tags Integrals
// @Summary 批量删除积分表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /integrals/deleteIntegralsByIds [delete]
func (integralsApi *IntegralsApi) DeleteIntegralsByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	err := integralsService.DeleteIntegralsByIds(ids)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateIntegrals 更新积分表
// @Tags Integrals
// @Summary 更新积分表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Integral.Integrals true "更新积分表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /integrals/updateIntegrals [put]
func (integralsApi *IntegralsApi) UpdateIntegrals(c *gin.Context) {
	var integrals Integral.Integrals
	err := c.ShouldBindJSON(&integrals)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = integralsService.UpdateIntegrals(integrals)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindIntegrals 用id查询积分表
// @Tags Integrals
// @Summary 用id查询积分表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query Integral.Integrals true "用id查询积分表"
// @Success 200 {object} response.Response{data=Integral.Integrals,msg=string} "查询成功"
// @Router /integrals/findIntegrals [get]
func (integralsApi *IntegralsApi) FindIntegrals(c *gin.Context) {
	id := c.Query("id")
	reintegrals, err := integralsService.GetIntegrals(id)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reintegrals, c)
}

// GetIntegralsList 分页获取积分表列表
// @Tags Integrals
// @Summary 分页获取积分表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query IntegralReq.IntegralsSearch true "分页获取积分表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /integrals/getIntegralsList [get]
func (integralsApi *IntegralsApi) GetIntegralsList(c *gin.Context) {
	var pageInfo IntegralReq.IntegralsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := integralsService.GetIntegralsInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetIntegralsPublic 不需要鉴权的积分表接口
// @Tags Integrals
// @Summary 不需要鉴权的积分表接口
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /integrals/getIntegralsPublic [get]
func (integralsApi *IntegralsApi) GetIntegralsPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	integralsService.GetIntegralsPublic()
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的积分表接口信息",
	}, "获取成功", c)
}
